//
//  ByLoginVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/20.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByLoginVC.h"

// view
#import "ByViewLoginProtocolAgreement.h"        // 同意协议

@interface ByLoginVC ()

@property (nonatomic, strong) UIScrollView *scroll;


@end

@implementation ByLoginVC
{
    UIImageView *_logoView;             // logo
    UITextField *_mobileField;          // 手机输入
    UILabel     *_mobileErrorLabel;     // 手机错误提醒
    UITextField *_codeField;            // 验证码输入
    UILabel     *_codeErrorLabel;       // 验证码错误提醒
    
    UIButton    *_fetchCodeButton;      // 获取验证码按钮
    
    UIButton    *_loginButton;          // 登录按钮
    
    // 同意协议
    ByViewLoginProtocolAgreement *_protocolView;
}

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"登录";

    [self addLeftBackBarButtonItem];
    
    [self createViews];
}

#pragma mark - 懒加载
- (UIScrollView *)scroll {
    
    if (!_scroll) {
        _scroll = [UIScrollView new];
        _scroll.showsVerticalScrollIndicator = false;
        _scroll.backgroundColor = BY_BG_COLOR;
        [self.view addSubview:_scroll];
        [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            if (@available(ios 11.0,*)) {
                make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
                make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            } else {
                make.left.right.bottom.equalTo(self.view);
            }
        }];
    }
    return _scroll;
}

#pragma mark - 创建视图
- (void)createViews {
    
    // logo
    _logoView = [UIImageView new];
    _logoView.backgroundColor = [UIColor redColor];
    [self.scroll addSubview:_logoView];
    [_logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(150, 100));
        make.centerX.equalTo(self.scroll);
        make.top.mas_equalTo(20);
    }];
    
    // 手机输入
    _mobileField = [UITextField new];
    _mobileField.backgroundColor = [UIColor whiteColor];
    _mobileField.placeholder = @"请输入手机号";
    _mobileField.font = FONT(16);
    _mobileField.textAlignment = NSTextAlignmentCenter;
    _mobileField.layer.cornerRadius = 4;
    [self.scroll addSubview:_mobileField];
    [_mobileField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_logoView.mas_bottom).offset(20);
        make.left.equalTo(self.scroll).offset(20);
        make.width.equalTo(self.scroll).offset(-40);
        make.height.mas_equalTo(50);
    }];
    
    // 手机错误提示
    _mobileErrorLabel = [[UILabel alloc] init];
    _mobileErrorLabel.adjustsFontSizeToFitWidth = true;
    _mobileErrorLabel.numberOfLines = 1;
    _mobileErrorLabel.font = FONT(14);
    _mobileErrorLabel.text = @"请输入正确手机号";
    _mobileErrorLabel.textAlignment = NSTextAlignmentCenter;
    _mobileErrorLabel.textColor = BY_RED_COLOR;
    _mobileErrorLabel.hidden = true;
    [self.scroll addSubview:_mobileErrorLabel];
    [_mobileErrorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mobileField.mas_left);
        make.width.equalTo(_mobileField.mas_width);
        make.top.equalTo(_mobileField.mas_bottom);
        make.height.mas_equalTo(30);
    }];
    
    
    // 验证码输入
    _codeField = [UITextField new];
    _codeField.backgroundColor = [UIColor whiteColor];
    _codeField.placeholder = @"请输入验证码";
    _codeField.font = FONT(16);
    _codeField.textAlignment = NSTextAlignmentCenter;
    _codeField.layer.cornerRadius = 4;
    [self.scroll addSubview:_codeField];
    [_codeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mobileErrorLabel.mas_bottom).offset(10);
        make.left.equalTo(_mobileField);
        make.width.equalTo(_mobileField).multipliedBy(2.0 / 3);
        make.height.equalTo(_mobileField);
    }];
    
    // 验证码错误提示
    _codeErrorLabel = [[UILabel alloc] init];
    _codeErrorLabel.adjustsFontSizeToFitWidth = true;
    _codeErrorLabel.numberOfLines = 1;
    _codeErrorLabel.font = FONT(14);
    _codeErrorLabel.text = @"验证码不正确";
    _codeErrorLabel.textAlignment = NSTextAlignmentCenter;
    _codeErrorLabel.textColor = BY_RED_COLOR;
    _codeErrorLabel.hidden = true;
    [self.scroll addSubview:_codeErrorLabel];
    [_codeErrorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_codeField);
        make.width.equalTo(_codeField);
        make.top.equalTo(_codeField.mas_bottom);
        make.height.equalTo(_mobileErrorLabel);
    }];
    
    // 验证码按钮
    _fetchCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _fetchCodeButton.backgroundColor = BY_THEME_COLOR;
    _fetchCodeButton.titleLabel.font = FONT(16);
    _fetchCodeButton.layer.cornerRadius = 4;
    [_fetchCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_fetchCodeButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateNormal];
    [_fetchCodeButton addTarget:self action:@selector(fetchCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.scroll addSubview:_fetchCodeButton];
    [_fetchCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_codeField.mas_right).offset(10);
        make.right.equalTo(_mobileField);
        make.top.equalTo(_codeField);
        make.height.equalTo(_codeField);
    }];
    
    // 登录按钮
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginButton.backgroundColor = BY_THEME_COLOR;
    _loginButton.titleLabel.font = FONT(16);
    _loginButton.layer.cornerRadius = 4;
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.scroll addSubview:_loginButton];
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mobileField);
        make.right.equalTo(_mobileField);
        make.top.equalTo(_codeErrorLabel.mas_bottom).offset(40);
        make.height.equalTo(_mobileField);
    }];
    
    // 协议
    _protocolView = [ByViewLoginProtocolAgreement new];
    [self.scroll addSubview:_protocolView];
    [_protocolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_loginButton);
        make.right.equalTo(_loginButton);
        make.top.equalTo(_loginButton.mas_bottom).offset(20);
        make.height.mas_equalTo(20);
        make.bottom.equalTo(self.scroll.mas_bottom).offset(-20);
    }];
}





#pragma mark - 点击事件
// 获取验证码
- (void)fetchCode:(UIButton *)button {
    
    
}
// 登录
- (void)login:(UIButton *)button {
    
    _mobileErrorLabel.hidden = _mobileField.text.length > 0;
    _codeErrorLabel.hidden = _codeField.text.length > 0;
    
    // 模拟登录
    [ByCommonCache setUserInfo:[ByModelUser new]];
    // dismiss
    [self dismissViewControllerAnimated:true completion:nil];
}


@end
